package com.dating.entities;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

import com.dating.libs.client.parttern.asa.ADbManager;
import com.dating.libs.client.parttern.asa.ADbObject;

public class HerDbManager extends ADbManager{
	public static final String TAG="HerDbManager";
	
	Context context;
	public static DataBaseHelper help;

	public static final String TABLE = "tbl_user";

	public static final String _id_username = "id_username";
	public static final String _fullname = "fullname";
	public static final String _gender = "gender";
	public static final String _birthday = "birthday";
	public static final String _email = "email";
	public static final String _link_image = "link_image";
	public static final String _type_account = "type_account";
	public static final String _address = "address";
	public static final String _latitude = "latitude";
	public static final String _longitude = "longitude";
	public static final String _current_address = "current_address";
	public static final String _phone = "phone";
	public static final String _nickname = "nickname";
	public static final String _category = "category";

	public static final String[] COLUMNS = { ADbManager._id, _id_username,
			_fullname, _gender, _birthday, _email, _link_image, _type_account,
			_address, _latitude, _longitude, _current_address, _phone,
			_nickname, _category };
	
	public HerDbManager(Context context, String TABLE, String[] COLUMNS) {
		super(context, TABLE, COLUMNS);
		// TODO Auto-generated constructor stub
		help=new DataBaseHelper(context);
		super.help=new DataBaseHelper(context);	//must call before init()
		super.init(context, help);	//IMPORTANT!!
	}

	@Override
	public ArrayList<Her> select() throws SQLiteException {
		ArrayList<Her> array = new ArrayList<Her>();
		SQLiteDatabase db = help.getReadableDatabase();
		Cursor cursor = db.query(TABLE, COLUMNS, null, null, null, null,
				ADbManager._id);
		if(cursor.getCount()==0){
			Log.e(TAG, "cursor is null");
			cursor.close();
			db.close();
			help.close();
			return array;
		}
		cursor.moveToFirst();
		do {
			int _id = cursor.getInt(cursor.getColumnIndex(ADbManager._id));
			int size = COLUMNS.length-1;
			String[] values = new String[size];
			for (int i = 0; i < size; ++i) {
				values[i] = cursor.getString(i + 1);
			}
			Her t = new Her(_id, values);
			array.add(t);
		} while (cursor.moveToNext());
		cursor.close();
		db.close();
		help.close();
		return array;
	}

	@Override
	public ADbObject select(int id) throws SQLiteException {
		Her t = null;
		if (id >= 0) {
			SQLiteDatabase db = help.getReadableDatabase();
			Cursor cursor = db.query(TABLE, COLUMNS, ADbManager._id + "='" + id
					+ "'", null, null, null, ADbManager._id);
			cursor.moveToFirst();
			
			if(cursor.getCount()==0){
				Log.e(TAG, "cursor is null");
				cursor.close();
				db.close();
				help.close();
				return t;
			}
			do {
				int _id = cursor.getInt(cursor.getColumnIndex(ADbManager._id));
				int size = COLUMNS.length-1;
				String[] values = new String[size];
				for (int i = 0; i < size; ++i) {
					values[i] = cursor.getString(i + 1);
				}
				t = new Her(_id, values);
			} while (cursor.moveToNext());
			cursor.close();
			db.close();
			help.close();
		}
		return t;
	}

	@Override
	public ArrayList<Her> select(String field, String value)
			throws SQLiteException {
		ArrayList<Her> array = new ArrayList<Her>();
		SQLiteDatabase db = help.getReadableDatabase();
		for(String tmp:COLUMNS)
			if(tmp.equals(field)){
				break;
			}else{
				Log.e(TAG, "not matching field to SELECT");
				return array;
			}
		Cursor cursor = db.query(TABLE, COLUMNS, field + "='" + value
				+ "'", null, null, null, ADbManager._id);
		
		if(cursor.getCount()==0){
			Log.e(TAG, "cursor is null");
			cursor.close();
			db.close();
			help.close();
			return array;
		}
		
		cursor.moveToFirst();
		do {
			int _id = cursor.getInt(cursor.getColumnIndex(ADbManager._id));
			int size = COLUMNS.length-1;
			String[] values = new String[size];
			for (int i = 0; i < size; ++i) {
				values[i] = cursor.getString(i + 1);
			}
			Her t = new Her(_id, values);
			array.add(t);
		} while (cursor.moveToNext());
		cursor.close();
		db.close();
		help.close();
		return array;
	}

}
